var vm = new Vue({
    el: "#zcJobList",
    data: {
        // 部位列表
        positionList:[],
        // 工单RFID
        order:null,
        // 工单id
        orderId: null,
        documentUrl:null //工艺指导文件路径
    },
    ready: function() {
    },
    methods: {
        // 显示图片
        showPhoto: function(id) {
            layer.photos({
                photos: '#' + id
                ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            })
        },
        // 部位完工，点击勾选
        hook: function(id, isMachining) {
            var _self = this
            var count = 0
            for (const n in this.positionList) {
                if (this.positionList[n].PBOM_ID === id) {
                    this.positionList[n].STATUS = 1 - isMachining
                    this.addPositionFinish(this.positionList[n].PBOM_ID, isMachining)
                }

                if (this.positionList[n].STATUS == 1) {
                    count ++
                }
            }

            if (this.positionList.length == count) {
                layer.confirm('工单在该环节已完工？', {
                    btn: ['是','否'], //按钮
                    closeBtn: 0
                }, function() {
                    // 完工
                    _self.$http.post(host +"manNeedleCart/processFinished",{"orderNo": _self.order},{emulateJSON: true}).then(function(res) {
                        if (res.ok && res.body.result) {
                            layer.msg("报工成功！");
                        } else {
                            layer.msg("报工失败！");
                        }
                    });
                }, function() {
                    // 暂不完工
                    for (const n in _self.positionList) {
                        if (_self.positionList[n].PBOM_ID === id) {
                            _self.positionList[n].STATUS = 0
                            _self.addPositionFinish(_self.positionList[n].PBOM_ID, 1)
                            break
                        }
                    }
                });
            }
        },
        // 显示指导文件
        showDocument: function() {
            if (null == this.order || '' == this.order) {
                layer.msg("请扫描RFID")
                return
            }

            layer.open({
                type: 2,
                title: '工艺指导文件',
                shadeClose: true,
                shade: 0.8,
                area: ['90%', '90%'],
                content: "../../script/js/pdfJs/web/viewer.html?file=../../../../manNeedleCart/downFile/" + this.order //iframe的url
            });
        },
        // 异常上报
        abnormalReport: function() {
            layer.open({
                type: 2,
                title: '异常上报',
                shadeClose: true,
                shade: 0.8,
                area: ['90%', '90%'],
                content: "abnormalReport.html" //iframe的url
            });
        },
        keydownCode: function(event) {
            if (event.keyCode == 13) {
                this.search();
            }
        },
        // 查找工单信息
        search: function() {
            var _self = this
            this.$http.post(host +"manNeedleCart/getPositionList",{"orderNo": this.order},{emulateJSON: true}).then(function(res) {
                if(res.ok  && res.body.result) {
                    _self.documentUrl = res.body.documentUrl
                    _self.positionList = res.body.positionList
                    for (const n in _self.positionList) {
                        _self.positionList[n].IMAGE = encodeURI(encodeURI(_self.positionList[n].IMAGE))
                    }

                    if (null != res.body.positionList && res.body.positionList.length > 0) {
                        _self.orderId = res.body.positionList[0].EMESO_JOB_ORDER_ID
                    } else {
                        _self.orderId = null
                    }

                    _self.showDocument()
                } else {
                    layer.msg(res.body.msg)
                }
            });
        },
        addPositionFinish: function(PBOM_ID, status) {
            this.$http.post(host +"manNeedleCart/addPositionFinished",{"orderId": this.orderId, "PBOM_ID": PBOM_ID, "status": status},{emulateJSON: true}).then(function(res) {
                if (res.ok && res.body.result) {
                } else {
                    layer.msg("部位状态修改失败！");
                }
            });
        }
    }
});